package cn.zmit.mydemo10;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiefan on 2016/6/20.
 */
public class FruitprofusionActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {
    private GridView gv_fruitprofusion;
    private List<String> list = new ArrayList<String>();
    private SwipeRefreshLayout swipeRefreshLayout;
    MyAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fruitprofusion);

        for (int temp = 0; temp < 30; temp++) {
            list.add("苹果");
        }

        adapter = new MyAdapter(FruitprofusionActivity.this, list);
//        adapter.notifyDataSetChanged();

        gv_fruitprofusion = (GridView) findViewById(R.id.gv_fruitprofusion);
        gv_fruitprofusion.setAdapter(adapter);

        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        swipeRefreshLayout.setOnRefreshListener(this);
//        swipeRefreshLayout.setRefreshing(true); //显示刷新进度条,此处加上这句导致无法刷新？？？
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
    }

    @Override
    public void onRefresh() {
        Log.i("Main", "刷新");
        list.add("香蕉");
//        for (int temp = 0; temp < 30; temp++) {
//            list.set(temp, "香蕉");
//        }
//        adapter.notifyDataSetChanged();
        adapter.notifyDataSetInvalidated();
        Log.i("Main", "list.size():" + list.size());
        Log.i("Main", "list():" + list);
        Toast.makeText(FruitprofusionActivity.this, "正在刷新", Toast.LENGTH_SHORT).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(FruitprofusionActivity.this, "刷新成功", Toast.LENGTH_SHORT).show();
                swipeRefreshLayout.setRefreshing(false);
            }
        }, 5000);

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                Toast.makeText(FruitprofusionActivity.this, "正在刷新", Toast.LENGTH_SHORT).show();
//               swipeRefreshLayout.setRefreshing(false);
//            }
//        }).start();

    }
}

class MyAdapter extends BaseAdapter {
    Context context;
    List<String> list;

    public MyAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder;
        if (view == null) {
            view = View.inflate(context, R.layout.item_grid_fruitprofusion, null);
            holder = new Holder();
            holder.tv = (TextView) view.findViewById(R.id.tv_fruitname);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        holder.tv.setText(list.get(i));

        return view;
    }

    class Holder {
        TextView tv;
    }
}
